//
//  
//  柳州微博
//
//  Created by Apple_Lzzy40 on 16/10/19.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "ZQStatueCell.h"
#import "UIImageView+WebCache.h"
#import "ZQUser.h"
#import "ZQStatusFrame.h"
#import "ZQPicture.h"
#import "ZQStatusToolbar.h"
#import "ZQStatusPhotosView.h"
#import "ZQIconView.h"
@interface ZQStatueCell()
/**原创微博的容器*/
@property(nonatomic,weak)UIView * originalView;
 /**头像*/
@property(nonatomic,strong)ZQIconView * iconView;
/** 会员图标 */
@property(nonatomic,weak)UIImageView * vipView;
/** 配图 */
@property(nonatomic,weak)ZQStatusPhotosView * photosView;
/** 昵称 */
@property(nonatomic,weak)UILabel * nameLabel;
/** 时间 */
@property(nonatomic,weak)UILabel * timeLabel;
/** 来源 */
@property(nonatomic,weak)UILabel * sourceLabel;
/** 正文 */
@property(nonatomic,weak)UILabel * contentLabel;

/**转发微博的容器*/
@property(nonatomic,weak)UIView * retweetView;
/** 转发微博的正文 */
@property(nonatomic,weak)UILabel * retweetContentLabel;
/** 转发微博的配图 */
@property(nonatomic,weak)ZQStatusPhotosView * retweetphotosView;
/**工具条*/
@property(nonatomic,weak)ZQStatusToolbar * toolbarview;



@end

@implementation ZQStatueCell
//Cell出实话类方法
+(instancetype)cellWithTabView:(UITableView *)tableview
{
    static NSString *cellID=@"statuses";
    //从循环次取cell
    ZQStatueCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID ];
    
    if (cell ==nil)//没有可用的cell，则创建
    {
        cell=[[ZQStatueCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    return cell;
}
/**
 *  相当于采购物品
 *
 *  @param style           <#style description#>
 *  @param reuseIdentifier <#reuseIdentifier description#>
 *
 *  @return sele
 */
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        /**原创微博*/
        [self steupOriginal];
        /** 转发微博 */
        [self steupRetweet];
        /**工具条*/
        [self setupToolbar];
    }
    return self;
  
}
//调整每个控件的frame
-(void)setFrame:(CGRect)frame
{
    frame.origin.y += 10;
    [super setFrame:frame];
}
 /**工具条*/
-(void)setupToolbar
{
    /**工具条*/
    //UIView  * toolbar=[[UIView alloc] init];
    //toolbar.backgroundColor=[UIColor redColor];
    ZQStatusToolbar  * toolbar=[[ZQStatusToolbar alloc] init];
    [self.contentView addSubview:toolbar];
    self.toolbarview=toolbar;
}
/** 转发微博 */
-(void)steupRetweet
{
    /** 转发微博的容器 */
    UIView * reweetedView = [[UIView alloc] init];
    [self.contentView addSubview:reweetedView];
    self.retweetView = reweetedView;
    
    /** 转发微博的正文 */
    UILabel * retweetContentLabel= [[UILabel alloc] init];
    retweetContentLabel.font=ZQStatusCellRetweetContentFont;
    retweetContentLabel.numberOfLines=0;
    [self.retweetView addSubview:retweetContentLabel];
    self.retweetContentLabel=retweetContentLabel;
    
    /** 转发微博的配图 */
    ZQStatusPhotosView *retweetePhotoView =[[ZQStatusPhotosView alloc] init];
    [self.retweetView addSubview:retweetePhotoView];
    self.retweetphotosView=retweetePhotoView;

}
/**原创微博*/
-(void)steupOriginal
{
    /**原创微博的容器*/
    UIView * originalView=[[UIView alloc] init];
    originalView.backgroundColor=[UIColor whiteColor];
    [self.contentView addSubview:originalView];
    self.originalView=originalView;
    
    /**头像*/
    ZQIconView *iconView=[[ZQIconView alloc] init];
    [originalView addSubview:iconView];
    self.iconView=iconView;
    
    //会员图标
    UIImageView * vipView=[[UIImageView alloc] init];
    [originalView addSubview:vipView];
    self.vipView=vipView;
    
    //配图
    ZQStatusPhotosView *photoView =[[ZQStatusPhotosView alloc] init];
    [originalView addSubview:photoView];
    self.photosView=photoView;
    
    //昵称
    UILabel *nameLabel =[[UILabel alloc] init];
    nameLabel.font=ZQStatusCellNameFont;
    [originalView addSubview:nameLabel];
    self.nameLabel=nameLabel;
    
    //时间
    UILabel *timeLabel =[[UILabel alloc] init];
    timeLabel.font=ZQStatusCellTimeFont;
    [originalView addSubview:timeLabel];
    self.timeLabel=timeLabel;
    
    //来源
    UILabel *soueceLabel =[[UILabel alloc] init];
    soueceLabel.font=ZQStatusCellSourceFont;
    [originalView addSubview:soueceLabel];
    self.sourceLabel=soueceLabel;
    
    //正文
    UILabel *contentLabel =[[UILabel alloc] init];
    contentLabel.font=ZQStatusCellContentFont;
    contentLabel.numberOfLines = 0;
    [originalView addSubview:contentLabel];
    self.contentLabel=contentLabel;


}

/**
 *  把物品安装到指定的位置
 *
 *  @param statusFrame 
 */
-(void)setStatusFrame:(ZQStatusFrame *)statusFrame
{
    _statusFrame=statusFrame;
    
    ZQUser * user =statusFrame.status.user;
   
    /** 原创微博的容器 */
    self.originalView.frame=_statusFrame.originalViewFrame;
    
    
    /**头像*/
    self.iconView.frame=_statusFrame.iconViewFrame;
    self.iconView.backgroundColor = [UIColor blueColor];
    self.iconView.user=user;
   
    /** 昵称 */
    self.nameLabel.text=user.name;
    self.nameLabel.frame=_statusFrame.nameLabelFrame;

    /** 会员图标 */
    if (user.isVip)
    {
        //mbrank
        self.vipView.height=NO;
        NSString *vipImageName=[NSString stringWithFormat:@"common_icon_membership_level%d",user.mbrank];
        
        self.vipView.image=[UIImage imageNamed:vipImageName];
        self.vipView.frame=_statusFrame.vipViewFrame;
        
    }else
    {
        self.vipView.height=YES;
    }
    /** 配图 */
    if (statusFrame.status.pic_urls.count) {
        self.photosView.frame=statusFrame.photosViewFrame;
       // ZQPicture * pic =[statusFrame.status.pic_urls firstObject];
//        [self.photosView sd_setImageWithURL:[NSURL URLWithString:pic.thumbnail_pic] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
        self.photosView.photos =statusFrame.status.pic_urls;
        
        self.photosView.hidden=NO;

    }else
    {
        self.photosView.hidden=YES;
    }
    /** 时间 */
     NSString *time =statusFrame.status.created_at;
    CGFloat timeX=_statusFrame.nameLabelFrame.origin.x;
    CGFloat timeY= CGRectGetMaxY(_statusFrame.nameLabelFrame)+ ZQStatusCellBorderWL;
   
    // 获得字符串的尺寸
        CGSize timeSize=[time sizeWithText:ZQStatusCellContentFont];
    self.timeLabel.frame=CGRectMake(timeX, timeY, timeSize.width, timeSize.height);
    self.timeLabel.text = time;
    
    /** 来源 */
    NSString *source =statusFrame.status.source;
    CGFloat sourceX=CGRectGetMaxX(self.timeLabel.frame)+ZQStatusCellBorderWL;
    CGFloat sourceY=timeY;
    // 获得字符串的尺寸
     CGSize sourceSize=[source sizeWithText:ZQStatusCellContentFont];
    self.sourceLabel.frame=CGRectMake(sourceX, sourceY, sourceSize.width, sourceSize.height);
    self.sourceLabel.text =source;

    /** 正文 */
    self.contentLabel.text=_statusFrame.status.text;
    self.contentLabel.frame=_statusFrame.contentLabelFrame;
    
    /** 转发微博 */
    if (statusFrame.status.retweeted_status) {
        
        //获取被转发微博对象或模型
        ZQStatus * retweeted_status=statusFrame.status.retweeted_status;
        //获取被转发微博用户对象或模型
        ZQUser *retweeted_status_user=retweeted_status.user;
        self.retweetView.hidden=NO;
        self.retweetView.frame=statusFrame.retweetedViewFrame;
        /** 转发微博  正文  用户名  ＋转发正文*/
        NSString *retweetContent =[NSString stringWithFormat:@" @%@ : %@",retweeted_status_user.name,retweeted_status.text];
        self.retweetContentLabel.text=retweetContent;
        self.retweetContentLabel.frame=statusFrame.retweetedContentLabelFrame;
        
        /** 转发配图 */
        if (retweeted_status.pic_urls.count) {
            self.retweetphotosView.frame=statusFrame.retweetedphotosViewFrame;
            //ZQPicture * pic =[retweeted_status.pic_urls firstObject];
//            
//            [self.retweetphotosView sd_setImageWithURL:[NSURL URLWithString:pic.thumbnail_pic] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
              self.retweetphotosView.photos =statusFrame.status.retweeted_status.pic_urls;
            self.retweetphotosView.hidden=NO;
        }else
        {
            self.retweetphotosView.hidden=YES;
        }
    
    }else
    {
    
        self.retweetView.hidden=YES;
    }
    /**工具条*/
    self.toolbarview.frame=statusFrame.toolbarFrame;
    self.toolbarview.status=statusFrame.status;
}



@end
